package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.constant.BrandRemoteConfigConstant;
import com.tbit.uqbike.enums.UsageTypeEnum;
import com.tbit.uqbike.object.pojo.BrandRemoteConfig;
import com.tbit.uqbike.object.pojo.RushPayFeeConfig;
import com.tbit.uqbike.object.pojo.vo.UsageMessageVO;
import com.tbit.uqbike.object.pojo.vo.UsageVO;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.UsageRemindService;
import com.tbit.uqbike.webmanager.dao.core.BrandRemoteConfigDao;
import com.tbit.uqbike.webmanager.dao.log.UsageRemindDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 用量提醒
 *
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2024-10-18
 */
@Service
@Slf4j
public class UsageRemindServiceImpl implements UsageRemindService {

    @Resource
    private UsageRemindDao usageRemindDao;
    @Resource
    private BrandRemoteConfigDao brandRemoteConfigDao;


    /**
     * 用量提醒弹窗内容
     *
     * @param brandId 品牌id
     * @return Result
     */
    @Override
    public Result<UsageMessageVO> getMessage(Integer brandId) {

        List<BrandRemoteConfig> brandRemoteConfigs = brandRemoteConfigDao.selectBrandRemoteConfigList(new BrandRemoteConfig(brandId, BrandRemoteConfigConstant.RUSH_PAY_FEE));
        if (CollectionUtil.isEmpty(brandRemoteConfigs)) {
            log.info("催缴配置为空");
            return Result.success();
        }
        String paramKV = brandRemoteConfigs.get(0).getParamKV();
        RushPayFeeConfig config;
        try {
             config = new RushPayFeeConfig().getRushPayFeeConfigByStr(paramKV);
             if (config.getUsageFlag() == 0){
                 return Result.success();
             }
        }catch (Exception e){
            return Result.error("催缴配置异常");
        }

        //获取对应品牌的sim卡到期数量和优驱API剩余用量
        List<UsageVO> usageList = usageRemindDao.selectUsage(brandId);
        StringBuilder serviceName = new StringBuilder();
        StringBuilder simName = new StringBuilder();
        String simUrl = "";
        for (UsageVO usageVO : usageList) {
            if (Objects.isNull(usageVO.getCurrentUsage())){
                continue;
            }

            if (!Objects.equals(usageVO.getType(), UsageTypeEnum.SIM.getValue())){
                if (config.getApiCount() == 0){
                    continue;
                }
                if (Objects.nonNull(usageVO.getPastUsage()) && usageVO.getCurrentUsage() < config.getApiCount() && !Objects.equals(usageVO.getPastUsage(), usageVO.getCurrentUsage())){
                    serviceName.append(UsageTypeEnum.fromValue(usageVO.getType()).getDescription()).append("，");
                    continue;
                }
                if (Objects.isNull(usageVO.getPastUsage()) && usageVO.getCurrentUsage() < config.getApiCount()){
                    serviceName.append(UsageTypeEnum.fromValue(usageVO.getType()).getDescription()).append("，");
                    continue;
                }
            }

            if (UsageTypeEnum.SIM.getValue() == usageVO.getType() && usageVO.getCurrentUsage() > 0){
                if (config.getSimExpirationTime() == 0){
                    continue;
                }
                simName.append(usageVO.getCurrentUsage()).append("张SIM卡即将到期/已到期");
                simUrl = usageVO.getUrl();
            }
        }


        StringBuilder message = new StringBuilder();
        if (StrUtil.isNotEmpty(serviceName) && StrUtil.isNotEmpty(simName)) {
            StringBuilder newServiceName = serviceName.deleteCharAt(serviceName.length() - 1);
            message.append("尊敬的客户您好，系统检测到您的优驱API(").append(newServiceName).append(")额度不足和有").append(simName).append("，请及时进行续费，避免相关服务停用，如有疑问请联系客服为您处理。");
        } else if (StrUtil.isNotEmpty(serviceName) && StrUtil.isEmpty(simName)) {
            StringBuilder newServiceName = serviceName.deleteCharAt(serviceName.length() - 1);
            message.append("尊敬的客户您好，系统检测到您的优驱API(").append(newServiceName).append(")额度不足，请及时进行续费，避免相关服务停用，如有疑问请联系客服为您处理。");
        } else if (StrUtil.isEmpty(serviceName) && StrUtil.isNotEmpty(simName)) {
            message.append("尊敬的客户您好，系统检测到有").append(simName).append("，请及时进行续费，避免相关服务停用，如有疑问请联系客服为您处理。");
        }

        return Result.success(new UsageMessageVO(message.toString(), StrUtil.isBlank(simUrl) ? null : simUrl));
    }


}
